<template>
  <div>
    <div style="margin-bottom: 20px;">
      <el-row :gutter="20">
        <el-col :span="4" v-for="(o, index) in planes" :key="index">
          <el-card :body-style="{ padding: '5px' }">
            <img src="../air-flight/Boeing747.png" class="image">
            <div style="padding: 14px;">
              <span>{{ o.planeName }}</span>
              <div class="bottom clearfix">
                <!-- 添加 @click 事件，传递 planeId 参数 -->
                <el-button type="link" class="button" @click="addFlight(o.planeId)">添加航班</el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 分页组件居中 -->
    <div style="display: flex; justify-content: center; margin-top: 20px;">
      <el-pagination
        v-if="total > 0"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        :page-count="pageCount" 
        :pager-count="5"  
        layout="prev, pager, next, jumper, ->, first, last"
        @current-change="handlePageChange"
        @size-change="handlePageSizeChange"
      />
    </div>
  </div>
</template>

<script lang="ts">
//import { ref, onMounted } from "vue";

//import airFlightRoutes from "./index";
import { getPlaneNotFlyApi } from "@/api/air_plane";  // 导入查询接口
import { AirPlaneQuery, AirPlaneVO, PlanePageResult } from "@/api/air_plane/types";  // 导入类型

export default {
  data() {
    return {
      planes: [] as AirPlaneVO[],  // 飞机列表
      total: 0,  // 总条数
      pageCount: 0, // 总页数
      queryParams: {
        pageNum: 1,
        pageSize: 12,  // 每页显示 12 条
        // status: "0",   // 假设查询空闲飞机
      } as AirPlaneQuery,
    };
  },
  methods: {
    // 查询空闲飞机
    async queryPlanes() {//异步请求
      try {
        const response = await getPlaneNotFlyApi(this.queryParams); // 请求后端数据
        const result = response.data;

        // 更新飞机列表和总条数
        this.planes = result.list;  // 使用返回的 list
        this.total = result.total;  // 使用 total 字段
        this.pageCount = Math.ceil(this.total / this.queryParams.pageSize);  // 计算总页数
      } catch (error) {
        console.error("查询空闲飞机信息失败:", error);
      }
    },

    // 页码变化时调用的处理函数
    handlePageChange(pageNum: number) {
      this.queryParams.pageNum = pageNum;  // 更新分页页码
      this.queryPlanes();  // 重新请求数据
    },

    // 每页数据条数变化时的处理函数
    handlePageSizeChange(pageSize: number) {
      this.queryParams.pageSize = pageSize;
      this.queryPlanes();  // 重新请求数据
    },

    // 添加航班跳转函数，传递 planeId 参数
    addFlight(planeId: string) {
      this.$router.push({ path: '/flight/addFlight', query: { planeId } });
    }
  },
  mounted() {
    this.queryPlanes();  // 页面加载时调用查询方法
  }
};
</script>

<style scoped>
  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 2;
    float: center;
  }

  .image {
    width: 190px;
    height: 190px;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
      display: table;
      content: "";
  }

  .clearfix:after {
      clear: both;
  }
</style>
